package com_.dxy_test_.fast_fail_;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Description 快速失败机制测试
 * @Author dangxianyue
 * @Date 2022/9/20 17:15
 */
public class FastFailTest {

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("a","1");
        map.put("b","2");
        map.put("c","3");
        map.put("d","4");

//        for (String key : map.keySet()) {
//            if ("b".equals(key)) {
//                map.remove(key);
//            }
//        }

        map.forEach((k,v) -> {
            if ("b".equals(k)) {
                map.remove(k);
            }
        });


        System.out.println(map);

    }

//运行结果： 抛出并发修改异常
/*	Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911)
	at java.util.ArrayList$Itr.next(ArrayList.java:861)
	at Yinyong.Main.main(Main.java:28)
*/

}
